﻿var page = new lg.page({
    data: {
        grades: null,
        currStu:"",
    },
    mounted: function () {
       
    },
    methods: {
        RenderGrades: function (success) {
            if (this.grades == null) {
                lg.request({
                    url: "/SysApi/GetGradesByDepth",
                    data: { depth: 2, fid: 0 },
                    success: res => {
                        if (res.code == 0) {
                            this.grades = res.data;
                            if (typeof success == "function") {
                                success(this.grades);
                            }
                        }
                    }
                })
            } else {
                if (typeof success == "function") {
                    success(this.grades);
                }
            }
        },
        ConsultList: function () {
            var that = this;
            layui.use(['form', 'table','laydate'], function () {
                var table = layui.table;
                var form = layui.form;
                var laydate = layui.laydate;
                form.render();
                that.RenderGrades(res => {
                    lg.select.fill({
                        data: res,
                        target: "#sch_Grade",
                        value: "Id",
                        text: "GradeName",
                        callback: function () {
                            form.render();
                        }
                    })
                })
                laydate.render({
                    elem: "#sch-Start",
                    type:"date"
                })
                laydate.render({
                    elem: "#sch-End",
                    type: "date"
                })
                page.laytable = table.render({
                    elem: '#Consult'
                    , url: '/SysApi/GetConsults'
                    , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'rownum', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        },
                        {
                            field: 'Name', title: '姓名',width:120
                        }
                        , {
                            field: 'Gender', title: '性别', width: 60,
                        }
                        , {
                            field: 'Phone', title: '手机', width: 130,
                        }
                        , {
                            field: 'CurrSchool', title: '当前就读学校', width: 120,
                        }
                        , {
                            field: 'GradeName', title: '年级', width: 70,
                        }
                        ,{
                            field: 'ComeWay', title: '来源', width: 60,
                        },
                        {
                            field: 'ConsultMode', title: '方式', width: 60,
                        }
                        ,{
                            field: 'Tips', title: '详情'
                        }
                        , {
                            field: 'AddTime', title: '日期', width: 170
                        }
                        , {
                            field: 'ReplyTimes', title: '回访', width: 100, templet: function (d) {
                                if (d.ReplyTimes > 0) {
                                    return '已回访(' + d.ReplyTimes + ')'
                                } else {
                                    return '待回访'
                                }
                            }
                        }
                        , {
                            field: 'State', title: '状态', width: 100
                        }
                        , { fixed: 'right', title: '操作', toolbar: '#btTpl', width: 150 }
                    ]],
                    page: true
                });
                //头工具栏事件
                table.on('toolbar(Consult)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'add':
                            page.go("/Lgm/AddConsult","#floatBody")
                            return false;
                            break;
                        case 'refresh':
                            page.laytable.reload();
                            break;
                        case 'export':
                            let param = lg.obj.toUrl(form.val("SForm"));
                            if (!lg.string.IsNullOrEmpty(param)) {
                                param = "?" + param;
                            }
                            LoadFile("/SysApi/ExportConsult" + param)
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(Consult)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('真的删除行么', function (index) {
                                lg.request({
                                    url: "/SysApi/DeleteConsult/" + data.Id,
                                    method:"post",
                                    success: res => {
                                        if (res.code == 0) {
                                            layer.msg("删除成功!", {icon:1})
                                            obj.del();
                                            layer.close(index);
                                        }
                                    }
                                })   
                            });
                            break;
                        case 'edit':
                            loadTpl({
                                url: "/Lgm/EditConsult/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                        case 'reply':
                            page.currStu=data;
                            page.go("/Lgm/Replys/" + data.SN, "#floatBody")
                            break;
                    }

                });
                form.on('submit(bt_Search)', function (data) {
                    page.laytable.reload({
                        where: data.field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                });
            });
        },
        AddConsult: function () {
            var form = layui.form;
            var that = this;
            var vue = new Vue({
                el: "#fm-AddConsult",
                data: {
                    ComeWay: "",
                }
            })
            form.render();
            this.RenderGrades(res => {
                lg.select.fill({
                    data: res,
                    target:"#GradeId",
                    value: "Id",
                    text: "GradeName",
                    callback: function () {
                        form.render();
                    }
                })
            })

            form.on("submit(AddConsult)", function (d) {
                var data = d.field;
                data.GradeName = that.grades.filter(p => p.Id == data.GradeId)[0].GradeName
                lg.request({
                    url: "/SysApi/AddConsult",
                    data: data,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("添加成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })

            form.on("select(ComeWay)", function (d) {
                vue.ComeWay = d.value;
            })

        },
        EditConsult: function (id) {
            var that = this;
            var form = layui.form;     
            var vue = new Vue({
                el: "#fm-EditConsult",
                data: {
                    ComeWay: "",
                    Recommender: "",
                    Agent:""
                }
            });
            this.RenderGrades(res => {
                lg.select.fill({
                    data: res,
                    target: "#GradeId",
                    value: "Id",
                    text: "GradeName",
                    callback: function () {
                        form.render();
                        lg.request({
                            url: "/SysApi/GetConsult/" + id,
                            success: function (d) {
                                if (d.code == 0) {
                                    if (typeof d.data == "object") {
                                        form.val("fm-EditConsult", d.data)
                                    }
                                    vue.Recommender = d.data.Recommender;
                                    vue.Agent = d.data.Agent;
                                    vue.ComeWay = d.data.ComeWay
                                }
                            }
                        })
                    }
                })
            })
            form.on("submit(EditConsult)", function (d) {
                var data = d.field;
                data.GradeName = that.grades.filter(p => p.Id == data.GradeId)[0].GradeName
                if (data.ComeWay != '推荐') { data.Recommender = '' };
                if (data.ComeWay != '代理') { data.Agent='' }
                lg.request({
                    url: "/SysApi/EditConsult",
                    data: data,
                    method: 'post',
                    success: function (d) {
                        if (d.code == 0) {
                            layer.msg("编辑成功！", { icon: 1 });
                            page.back(true);
                        } else {
                            layer.msg(d.msg, { icon: 2 })
                        }
                    }
                })
                return false;
            })
            form.on("select(ComeWay)", function (d) {
                vue.ComeWay = d.value;
            })
        },
        ReplyList: function () {
            var that = this;
            layui.use(['form', 'table','laydate'], function () {
                var table = layui.table;
                var form = layui.form;
                var laydate = layui.laydate;
                that.RenderGrades(res => {
                    lg.select.fill({
                        data: res,
                        target: "#sch_Grade",
                        value: "Id",
                        text: "GradeName",
                        callback: function () {
                            form.render();
                        }
                    })
                })
                laydate.render({
                    elem: "#sch-Start",
                    type: "date"
                })
                laydate.render({
                    elem: "#sch-End",
                    type: "date"
                })
                page.laytable = table.render({
                    elem: '#ReturnVisit'
                    , url: '/SysApi/GetReturnVisits'
                    , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'rownum', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        },
                        {
                            field: 'Name', title: '学生', width: 100
                        },
                        {
                            field: 'Detail', title: '回访记录'
                        }
                        , {
                            field: 'ReplyMode', title: '方式', width: 120
                        }
                        , {
                            field: 'Account', title: '回访人', width: 120
                        }  
                        , {
                            field: 'AddTime', title: '日期', width: 170
                        }
                    ]],
                    page: true
                });
                //头工具栏事件
                table.on('toolbar(Consult)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'add':
                            page.go("/Lgm/AddConsult", "#floatBody")
                            return false;
                            break;
                        case 'refresh':
                            page.laytable.reload();
                            break;
                    };
                });
                //监听行工具事件
                table.on('tool(Consult)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('真的删除行么', function (index) {
                                obj.del();
                                layer.close(index);
                            });
                            break;
                        case 'edit':
                            loadTpl({
                                url: "/Lgm/EditConsult/" + data.Id,
                                target: "#floatBody"
                            })
                            break;
                    }

                });
                form.on('submit(bt_Search)', function (data) {
                    page.laytable.reload({
                        where: data.field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                });
            });
        },
        Replys: function (sn) {
            layui.use(['form', 'table'], function () {
                var table = layui.table;
                var form = layui.form;
               
                page.ReplysTable = table.render({
                    elem: '#Replys'
                    , url: '/SysApi/GetReturnVisits'
                    , toolbar: '#toolbar-replys' //开启头部工具栏，并为其绑定左侧模板
                    , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    , cols: [[
                        {
                            field: 'rownum', title: '#', width: 60, templet: function (d) {
                                return d.LAY_TABLE_INDEX + 1
                            }
                        },
                        {
                            field: 'Name', title: '学生', width: 100
                        },
                        {
                            field: 'Detail', title: '回访记录'
                        }
                        , {
                            field: 'ReplyMode', title: '方式', width: 120
                        }
                        , {
                            field: 'Account', title: '回访人', width: 120
                        }
                        , {
                            field: 'AddTime', title: '日期', width: 170
                        }
                        , { fixed: 'right', title: '操作', toolbar: '#btTpl-replys', width: 150 }
                    ]],
                    where: {ConsultSN:sn},
                    page: true
                });
                //监听行工具事件
                table.on('tool(Replys)', function (obj) {
                    var data = obj.data;
                    //console.log(obj)
                    switch (obj.event) {
                        case 'del':
                            layer.confirm('真的删除行么', function (index) {
                                obj.del();
                                layer.close(index);
                            });
                            break;
                        case 'edit':
                            layer.open({
                                type: 2,
                                area: ['400px', '400px'],
                                title:'['+data.Name+"]",
                                content: "/Lgm/EditReturnVisit/" + data.Id,
                            })
                            break;
                        case 'reply':
                            page.go("/Lgm/Replys/" + data.SN, "#floatBody")
                            break;
                    }

                });
                //头工具栏事件
                table.on('toolbar(Replys)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'export':
                            break;
                        case 'refresh':
                            page.ReplysTable.reload();
                            break;
                        case 'add':
                            layer.open({
                                type: 2,
                                title: '[' + page.currStu.Name + "]",
                                area: ['400px', '400px'],
                                content: "/Lgm/AddReturnVisit/" + sn,
                            })
                            break;
                    };
                });

                form.on('submit(bt_Search)', function (data) {
                    page.laytable.reload({
                        where: data.field,
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                });
            });
        },
        AddReply: function () {
            layui.use(['form'], function () {
                var form = layui.form;
                form.on("submit(AddReply)", function (d) {
                    var data = d.field;
                    lg.request({
                        url: "/SysApi/AddReturnVisit",
                        data: data,
                        method: 'post',
                        success: function (d) {
                            if (d.code == 0) {
                                top.layer.closeAll();
                                top.layer.msg("添加成功！", { icon: 1 });
                                top.page.ReplysTable.reload();
                            } else {
                                top.layer.closeAll();
                                top.layer.msg(d.msg, { icon: 2 })
                            }
                        }
                    })
                    return false;
                })
               
            });
        },
        EditReply: function (id) {
            layui.use(['form'], function () {
                var form = layui.form;
                form.render();
                new Promise(function (resolve, reject) {
                    lg.request({
                        url: "/SysApi/GetReturnVisit/" + id,
                        success: res => {
                            if (res.code == 0) {
                                resolve(res.data)
                            }
                        }
                    })
                }).then(res => {
                    form.val("fm-EditReply",res)
                })
                form.on("submit(EditReply)", function (d) {
                    var data = d.field;
                    lg.request({
                        url: "/SysApi/EditReturnVisit",
                        data: data,
                        method: 'post',
                        success: function (d) {
                            if (d.code == 0) {
                                top.layer.closeAll();
                                top.layer.msg("修改成功！", { icon: 1 });
                                top.page.ReplysTable.reload();
                            } else {
                                top.layer.closeAll();
                                top.layer.msg(d.msg, { icon: 2 })
                            }
                        }
                    })
                    return false;
                })

            });
        }
    }
});